package com.example.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

import android.util.Log;

public class Protocol {
	final static int USER_CONNECT = 0;
	final static int DEVICE_CONNECT = 1;
	final static int USER_NAME = 2;
	final static int USER_PASSWORD = 3;
	final static int USER_SELECT = 4;
	final static int USER_STATUS = 5;
	final static int USER_PROGRESS = 6;
	final static int USER_STATUS_ACK=7;
	final static int USER_LOGIN_ACK=8;
	final static int LIGHTDEVICE=9;
	final static int STATUSDATABASE=10;
	final static int TIMEON=11;
	final static int TIMEOFF=12;
	final static int LIGHT_ACK=13;
//	protected BufferedReader dataReceiver = new BufferedReader();
	private int type;

	public String packMessages(String mgs, int type) {
		String out = "";
		switch (type) {
		case USER_CONNECT:
			out = "<USER_CONNECT>" + mgs + "</USER_CONNECT>";
			break;
		case 1:
			out = "<DEVICE_CONNECT>" + mgs + "</DEVICE_CONNECT>";
			break;
		case USER_NAME:
			out = "<USER_NAME>" + mgs + "</USER_NAME>";
			break;
		case USER_PASSWORD:
			out = "<USER_PASSWORD>" + mgs + "</USER_PASSWORD>";
			break;
		case USER_SELECT:
			out = "<USER_SELECT>" + mgs + "</USER_SELECT>";
			break;
		case USER_STATUS:
			out = "<USER_STATUS>" + mgs + "</USER_STATUS>";
			break;
		case USER_PROGRESS:
			out = "<USER_PROGRESS>" + mgs + "</USER_PROGRESS>";
			break;
		case USER_STATUS_ACK:
			out="<USER_STATUS_ACK>"+mgs+ "</USER_STATUS_ACK>";
			break;
		case USER_LOGIN_ACK:
			out="<USER_LOGIN_ACK=>"+mgs+"</USER_LOGIN_ACK>";
			break;
		case LIGHTDEVICE:
			out="<LIGHTDEVICE>"+mgs+"</LIGHTDEVICE>";
			break;
		case TIMEON:
			out ="<TIMEON>"+ mgs +"</TIMEON>";
			break;
		case TIMEOFF:
			out ="<TIMEOFF>"+ mgs +"</TIMEOFF>";
			break;

		}
		return out;
	}

	public void sendMessages(Socket socket, String mgs, int type)
			throws IOException {
		OutputStreamWriter out = new OutputStreamWriter(
				socket.getOutputStream());
		out.write(packMessages(mgs, type));
		Log.i("send",packMessages(mgs,type));
		out.flush();

	}

	public String extractData(String packet) {
		this.type = -1;
		if (packet.startsWith("<USER_CONNECT>") == true
				&& packet.endsWith("</USER_CONNECT>") == true) {
			this.type = USER_CONNECT;
			packet = "";

		}
		if (packet.startsWith("<LIGHT_ACK>") == true
				&& packet.endsWith("</LIGHT_ACK>") == true) {
			this.type = LIGHT_ACK;
			packet = "";

		}
		if (packet.startsWith("<USER_NAME>") == true
				&& packet.endsWith("</USER_NAME>") == true) {
			this.type = USER_NAME;
			packet = packet.substring(11, packet.length() - 12);

		}
		if (packet.startsWith("<USER_PASSWORD>") == true
				&& packet.endsWith("</USER_PASSWORD>") == true) {
			this.type = USER_PASSWORD;
			packet = packet.substring(14, packet.length() - 15);

		}
		if (packet.startsWith("<USER_SELECT>") == true
				&& packet.endsWith("</USER_SELECT>") == true) {
			this.type = USER_SELECT;
			packet = packet.substring(13, packet.length() - 14);

		}
		if (packet.startsWith("<USER_STATUS>") == true
				&& packet.endsWith("</USER_STATUS>") == true) {
			this.type = USER_STATUS;
			packet = packet.substring(13, packet.length() - 14);

		}
		if (packet.startsWith("<USER_PROGRESS>") == true
				&& packet.endsWith("</USER_PROGRESS>") == true) {
			this.type = USER_PROGRESS;
			packet = packet.substring(15, packet.length() - 16);

		}
		if(packet.startsWith("<USER_STATUS_ACK>")==true && packet.endsWith("</USER_STATUS_ACK>")){
			this.type=USER_STATUS_ACK;
			packet= "";
		} 
		if(packet.startsWith("<USER_LOGIN_ACK>")==true && packet.endsWith("</USER_LOGIN_ACK>")){
			this.type=USER_LOGIN_ACK;
			packet = packet.substring(16, packet.length() - 17);
		} 
		if(packet.startsWith("<STATUSDATABASE>")==true && packet.endsWith("</STATUSDATABASE>")){
			this.type=STATUSDATABASE;
			packet = packet.substring(16, packet.length() - 17);
		} 
		if (packet.startsWith("<USER_CONNECT>") == true
				&& packet.endsWith("</USER_CONNECT>") == true) {
			this.type = USER_CONNECT;
			packet = "";

		}
		return packet;
	}

	public String Receive(Socket sc) throws IOException {

		BufferedReader dataReceiver = new BufferedReader(new InputStreamReader(
				sc.getInputStream()));
		Character data;
		String packData = "";
		boolean loop = true;
		while (loop == true) {
			data = (char) dataReceiver.read();
			if (data != null) {
				packData = packData + data;
				Log.i("packdata",packData);
				// this.mainFrame.setTextArea(data.toString());
			}
			if (data.equals('>') == true) {
				if (packData.equals(this.extractData(packData)) == false) {
					loop = false;
					
				}
			}

		}
        Log.i("hic", packData);
		return packData;
	}
	public int findTypeMsg(String packet) {
		int type = -1;
		if (packet.startsWith("<USER_CONNECT>") == true
				&& packet.endsWith("</USER_CONNECT>") == true) {
			type = USER_CONNECT;
			packet = "";

		}
		if (packet.startsWith("<DEVICE_CONNECT>") == true
				&& packet.endsWith("</DEVICE_CONNECT>") == true) {
			type = DEVICE_CONNECT;
			packet = "";

		}
		if (packet.startsWith("<USER_NAME>") == true
				&& packet.endsWith("</USER_NAME>") == true) {
			type = USER_NAME;
			packet = packet.substring(10, packet.length() - 11);

		}
		if (packet.startsWith("<USER_PASSWORD>") == true
				&& packet.endsWith("</USER_PASSWORD>") == true) {
			type = USER_PASSWORD;
			packet = packet.substring(13, packet.length() - 14);

		}
		if (packet.startsWith("<USER_SELECT>") == true
				&& packet.endsWith("</USER_SELECT>") == true) {
			type = USER_SELECT;
			packet = packet.substring(12, packet.length() - 13);

		}
		if (packet.startsWith("<USER_STATUS>") == true
				&& packet.endsWith("</USER_STATUS>") == true) {
			type = USER_STATUS;
			packet = packet.substring(12, packet.length() - 13);

		}
		if (packet.startsWith("<USER_PROGRESS>") == true
				&& packet.endsWith("</USER_PROGRESS>") == true) {
			type = USER_PROGRESS;
			packet = packet.substring(14, packet.length() - 14);

		}
		if(packet.startsWith("<USER_STATUS_ACK>")==true && packet.endsWith("</USER_STATUS_ACK>")){
			type=USER_STATUS_ACK;
			packet= "";
		}
		if(packet.startsWith("<USER_LOGIN_ACK>")==true && packet.endsWith("</USER_LOGIN_ACK>")){
			   type=USER_LOGIN_ACK;
			packet = packet.substring(16, packet.length() - 17);
		} 
		if(packet.startsWith("<STATUSDATABASE>")==true && packet.endsWith("</STATUSDATABASE>")){
			type=STATUSDATABASE;
			packet = packet.substring(16, packet.length() - 17);
		} 
		return type;
	}
}
